/**  
 * All rights Reserved, Designed By www.woniuxy.com
 * @Title:  Account.java   
 * @Package com.woniu.domain   
 * @Description:    Description  
 * @author: 蜗牛学院  
 * @date:   2019年7月24日 下午9:29:40
 * @user:   Administrator  
 * @version V1.0 
 * @Copyright: 2019 www.woniuxy.com Inc. All rights reserved. 
 * 注意：本内容仅限于成都蜗牛创想科技有限公司，禁止外泄以及用于其他的商业目
 */ 
package com.woniu.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.woniu.dao.StuDao;
import com.woniu.domain.Stu;
import com.woniu.utils.DataSourceUtils;

/**
 * @author Administrator
 *
 */
public class StuDaoImpl implements StuDao {
	QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
	@Override
	public List<Stu> ListStus() throws SQLException {
		
		return runner.query("select sid,sname,sgender,sage,shobby from stum", new BeanListHandler<Stu>(Stu.class));
	}

	@Override
	public int saveStu(Stu stu) throws SQLException {		
		return runner.execute("insert into stum values(?,?,?,?,?)", stu.getsId(),stu.getsName(),stu.getsGender(),stu.getsAge(),stu.getsHobby());
	}

	@Override
	public void delStu(Integer sId) throws SQLException {
		runner.execute("delete from stum where sid = ?", sId);
		
	}

	@Override
	public Stu findStu(Integer sId) throws SQLException {
		return runner.query("select sid,sname,sgender,sage,shobby from stum where sid = ?", new BeanHandler<Stu>(Stu.class), sId);
	
	}

	@Override
	public int updateStu(Stu stu) throws SQLException {
		
		return runner.execute("update stum set sname = ?,sgender = ?,sage = ?,shobby = ? where sId = ?", stu.getsName(),stu.getsGender(),stu.getsAge(),stu.getsHobby(),stu.getsId());
	}

	@Override
	public List<Stu> fingStu(String sql) throws SQLException {
		
		return runner.query(sql, new BeanListHandler<Stu>(Stu.class));
	}

	@Override
	public List<Stu> queryStuByPage(int currentPage, int pageSize) throws SQLException {
		return runner.query("select sid,sname,sgender,sage,shobby from stum limit ? offset ?", new BeanListHandler<Stu>(Stu.class),pageSize,(currentPage-1)*pageSize);
	}

	@Override
	public int countStus() throws SQLException{		
		return runner.query("select count(*) from stum", new ScalarHandler<Long>()).intValue();
	}

}
